/**
 * @author Jerry
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
package com.haoxi.gateway.runner;

/**
 * @author jerry
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
/*
 * here is the monitor resource control code
 */

public class QueueThreadCtl {
    private int readyNum = 0;

    private int runningNum = 0;

    private int AllNum = 0;

    private int DeadNum = 0;

    private int CouldRunning = 1;//初始设置。默认4，最大99

    private int MaxNum = 1;//促使设置。默认9，最大99

    private String[] nameAry;

    private int[] status;
    
    
    public QueueThreadCtl(int inMaxNum, int inCouldRunNum) {
        if (inMaxNum > 1) {
            MaxNum = inMaxNum;
        }
        if (inCouldRunNum > 1) {
            CouldRunning = inCouldRunNum;
        }

        nameAry = new String[(MaxNum + 1)];
        status = new int[(MaxNum + 1)];

        for (int t = 0; t < MaxNum; t++) {
            nameAry[t] = "X-0" + t + "C";
            status[t] = 0;
        }
    }

    synchronized public void AddReadyNum(int i) {
        status[i] = 1;//写成READY状态
        readyNum = readyNum + 1;
        AllNum = AllNum + 1;
    }

    synchronized public void AddRunningNum(int i) {
        status[i] = 2;//写成Running状态
        runningNum = runningNum + 1;
        readyNum = readyNum - 1;
    }

    synchronized public void SubRunningNum(int i) {
        status[i] = 1;//写成READY状态
        runningNum = runningNum - 1;
        readyNum = readyNum + 1;
        notifyAll();
    }

    synchronized public void AddDeadOne(int i) {
        status[i] = 0;//写成Dead状态
        runningNum = runningNum - 1;
        readyNum = readyNum - 1;
        AllNum = AllNum - 1;
        DeadNum = DeadNum + 1;
        notifyAll();
    }

    synchronized public void AddCouldRunNum(int i) {
        if (CouldRunning + i <= MaxNum) {
            CouldRunning = CouldRunning + i;
        }
    }

    synchronized public void SubCouldRunNum(int i) {
        if (CouldRunning - i > 1) {
            CouldRunning = CouldRunning - i;
        }
    }

    synchronized public int GetAllNum() {
        return AllNum;
    }

    synchronized public int GetReadyNum() {
        return readyNum;
    }

    synchronized public int GetRunningNum() {
        return runningNum;
    }

    synchronized public int GetMaxNum() {
        return MaxNum;
    }

    synchronized public String GetThreadName(int i) {
        return nameAry[i];
    }

    synchronized public boolean ApplyNewThread() {
        boolean ret = false;
        if (AllNum < MaxNum) {
            ret = true;
        }
        return ret;
    }

    synchronized public void CouldRun(int i) {
        while (runningNum >= CouldRunning) {
            try {
                wait();
            } catch (Exception ea) {
                System.out.println("异常.");

            }
        }
        runningNum = runningNum + 1;
        readyNum = readyNum - 1;
        status[i] = 2;//写成Running状态
    }

    synchronized public int GetDeadNum() {
        int ret = -1;
        if (DeadNum > 0) {
            for (int j = 0; j < MaxNum; j++) {
                if (status[j] == 0) {
                    ret = j;
                    break;
                }
            }
        }
        return ret;
    }
}
